(function() {
	'use strict';
	var sc = angular.module('sc');
	sc.controller('WorkExpCtrl', WorkExpCtrl)
	function WorkExpCtrl($http) {
		var ctrl = this
		this.addWorkExp = function(exp) {
			return $http.post(':sc:workExp/addWorkExp', exp).success(function(r) {
				return r;
			})
		}
		this.removeWorkExp = function(id) {
			return $http.post(':sc:workExp/removeWorkExp', {
				id : id
			}).success(function(r) {
				return r;
			})
		}
		this.updateWorkExp = function(exp) {
			return $http.post(':sc:workExp/updateWorkExp', exp).success(function(r) {
				return r;
			})
		}
		this.submitWorkExp = function(exp) {
			return $http.post(':sc:workExp/submitWorkExp', exp).success(function(r) {
				return r;
			})
		}

		this.queryWorkExp = function(student, type, status, grade, major, department, sex, pageNumber, pageSize) {
			return $http.post(':sc:workExp/queryWorkExp', {
				student : student,
				grade : grade,
				major : major,
				department : department,
				sex : sex,
				type : type,
				pageNumber : pageNumber,
				pageSize : pageSize,
				status : status
			}).success(function(r) {
				ctrl.result = r
				return r;
			})
		}

		this.queryWorkExpCount = function(student, type, status, grade, major, department, sex) {
			return $http.post(':sc:workExp/queryWorkExpCount', {
				student : student,
				grade : grade,
				major : major,
				department : department,
				sex : sex,
				type : type,
				status : status
			}).success(function(r) {
				ctrl.resultCount = r
				return r;
			})
		}
		this.getExpTypes = function() {
			return $http.post(':sc:workExp/getExpTypes').success(function(r) {
				ctrl.types = r
				return r;
			})
		}
		this.getWorkExps = function(studentId, type, status) {
			return $http.post(':sc:workExp/getWorkExps', {
				student : studentId,
				type : type,
				status : status
			}).success(function(r) {
				ctrl.workExps = r
				return r;
			})
		}
		this.getExpCount = function(student, status) {
			return $http.post(':sc:workExp/getWorkExpCount', {
				student : student,
				status : status
			}).success(function(r) {
				return r;
			})
		}

	}
	sc.controller('WorkExpOperateCtrl', WorkExpOperateCtrl)
	function WorkExpOperateCtrl($scope, $element, $controller, modalService, toaster, $http) {
		var workExpCtrl, ctrl = this, fileInit, uploadCtrl
		ctrl.reloadType = function() {
		}
		this.attachMappings = {
			'actExp' : {
				isImage : false,
				label : '上传成果材料',
				required : false
			},
			'award' : {
				isImage : true,
				description : '1.证明材料必须要上传哦，上传图片格式，且大小不要超过2M。<br>2.团队类型奖项必须填写“所属团队/承担任务”，个人奖项无需填写',
				label : '上传证明材料',
				required : true
			},
			'learnExp' : {
				isImage : false,
				label : '上传成果材料',
				required : false
			},
			'otherAct' : {
				isImage : false,
				label : '上传成果材料',
				required : false
			},
			'politicalExp' : {
				isImage : true,
				description : '1.证明材料必须要上传哦，上传图片格式，且大小不要超过2M。',
				label : '上传证明材料',
				required : true
			},
			'serviceExp' : {
				isImage : false,
				label : '上传成果材料',
				required : false
			},
			'socialExp' : {
				isImage : false,
				label : '上传成果材料',
				required : false
			},
			'studyExp' : {
				isImage : true,
				description : '1.证明材料必须要上传哦，上传图片格式，且大小不要超过2M。',
				label : '上传证明材料',
				required : true
			},
			'workExp' : {
				isImage : true,
				description : '1.证明材料必须要上传哦，上传图片格式，且大小不要超过2M。',
				label : '上传证明材料',
				required : true
			}
		}
		angular.forEach(this.attachMappings, function(v, k) {
			v.title = v.label.substring(2)
		})
		this.getMappings = function() {
			return ctrl.attachMappings;
		}
		this.init = function() {
			var locals = {
				'$scope' : $scope,
				'$element' : $element
			}
			workExpCtrl = $controller('WorkExpCtrl', locals)
			uploadCtrl = $controller('UploadCtrl', locals)
			var q1 = workExpCtrl.getExpTypes().success(function(r) {
				ctrl.types = r
				ctrl.typeMap = {}
				angular.forEach(r, function(v) {
					ctrl.typeMap[v.id] = v
				})
				return r;
			})
			fileInit = $http.post(':sc:attach/getServerWeb').success(function(r) {
				return uploadCtrl.init(r);
			})
			return q1;
		}
		function ExpModal(cfg) {
			var tpl = $element.find('.tpl.edit').clone().removeAttr('ng-non-bindable')
			var browser = tpl.find('.btn.file')
			var stream, files = cfg.result.attachs = (cfg.result.attachs || [])
			var fileMap = {}
			var mapping = ctrl.attachMappings[cfg.type]
			var isImage = mapping.isImage
			var extFilters = isImage ? [ ".jpg", ".jpeg", ".png", ".gif", ".bmp" ] : []
			var maxSize = isImage ? 1024 * 1024 * 2 : undefined;
			fileInit.then(function() {
				stream = uploadCtrl.create({
					autoUploading : true,
					browser : browser[0],
					maxSize : maxSize,
					onComplete : function(file) {
						var attach = fileMap[file.id] || {}
						attach.fileName = file.name;
						attach.hash = eval('(' + file.msg + ')').message;
						attach.size = file.size;
						attach.uploading = false
						$scope.$$phase || $scope.$apply()
					},
					onAddTask : function(file) {
						var attach = {
							fileName : file.name,
							size : file.size,
							uploading : true,
							fileId : file.id
						}
						fileMap[file.id] = attach
						files.push(attach)
						$scope.$$phase || $scope.$apply()
					},
					onSelect : function(list) {
						var notImage = false, overSize = false
						angular.forEach(list, function(v) {
							var i = v.type.indexOf('image')
							if (i != 0) {
								notImage = true
							}
							if (v.size > (1024 * 1024 * 2)) {
								overSize = true
							}
						})
						if (notImage && isImage) {
							toaster.pop({
								type : 'error',
								body : '只能添加图片文件哦',
								timeout : 800
							});
							$scope.$$phase || $scope.$apply()
						} else if (overSize && isImage) {
							toaster.pop({
								type : 'error',
								body : '图片大小不能超过2M',
								timeout : 800
							});
							$scope.$$phase || $scope.$apply()
						}
					},
					extFilters : extFilters
				})
				var modalCfg = {
					title : cfg.title,
					content : tpl,
					type : cfg.type,
					result : cfg.result,
					parentCtrl : ctrl,
					mapping : mapping,
					attachParam : {
						mapping : mapping,
						attachs : files
					},
					confirmText : '保存',
					size : 'lg',
					validator : function(r) {
						if (r.attachs) {
							for ( var i in r.attachs) {
								if (r.attachs[i].uploading) {
									toaster.pop({
										type : 'error',
										body : '还有未上传完的文件， 请等待文件上传完毕或者将其删除',
										timeout : 1500
									});
									return false;
								}
							}
						}
						if (!r.attachs || r.attachs.length == 0 || !mapping.required) {
							return true;
						}
						return true;
					}
				}
				modalCfg.delFile = function($index) {
					var f = files.splice($index, 1)
					stream.cancelOne(f[0].fileId)
				}
				var m = modalService.confirm(modalCfg, {
					closeOnOutsideClick : false
				}).then(cfg.callback)['finally'](function() {
					stream.destroy()
				})
			})
		}
		this.addExp = function(type, exp) {
			var result = {
				type : type.id,
				attachs : []
			}
			angular.extend(result, exp);
			var cfg = {
				title : '添加' + type.name,
				type : type.id,
				result : result

			}
			cfg.callback = function(r) {
				workExpCtrl.addWorkExp(r).success(function(r) {
					// modalService.succ('添加成功')
					toaster.pop({
						type : 'success',
						body : '添加成功',
						timeout : 1500
					});
					ctrl.reloadType(type.id)
				})
			}
			ExpModal(cfg)
		}
		this.editWorkExp = function(exp) {
			var e = angular.copy(exp)
			var cfg = {
				title : '编辑' + ctrl.typeMap[e.type].name,
				type : e.type,
				result : e
			}
			cfg.callback = function(r) {
				workExpCtrl.updateWorkExp(r).success(function(r) {
					// modalService.succ('修改成功')
					toaster.pop({
						type : 'success',
						body : '修改成功',
						timeout : 1500
					});
					ctrl.reloadType(e.type)
				})
			}
			ExpModal(cfg)
		}
		this.delWorkExp = function(exp) {
			modalService.confirm({
				title : '删除',
				content : '确定删除' + (exp.expName || '此条记录') + '吗？',
				result : exp
			}).then(function(r) {
				workExpCtrl.removeWorkExp(r.id).success(function(r) {
					// modalService.succ('删除成功')
					toaster.pop({
						type : 'success',
						body : '删除成功',
						timeout : 1500
					});
					ctrl.reloadType(exp.type)
				})
			})
		}
		this.submitWorkExp = function(exp) {
			var mapping = ctrl.attachMappings[exp.type]
			if ((!exp.attachs || exp.attachs.length == 0) && mapping.required) {
				toaster.pop({
					type : 'error',
					body : '请先上传证明文件后再提交',
					timeout : 1000
				});
				return;
			}
			modalService.confirm({
				title : '提交',
				content : '确定提交' + (exp.expName || '此条记录') + '吗？',
				result : exp
			}).then(function(r) {
				workExpCtrl.submitWorkExp(exp).success(function(r) {
					// modalService.succ('提交成功')
					toaster.pop({
						type : 'success',
						body : '提交成功',
						timeout : 1500
					});
					ctrl.reloadType(exp.type)
				})
			})
		}

		this.viewWorkExp = function(exp) {
			var mapping = ctrl.attachMappings[exp.type]
			$http.post(':sc:audit/getAudits', {
				id : exp.id
			}).success(function(r) {
				var tpl = $element.find('.tpl.view').clone().removeAttr('ng-non-bindable')
				modalService.confirm({
					title : '查看' + ctrl.typeMap[exp.type].name,
					content : tpl,
					type : exp.type,
					result : exp,
					parentCtrl : ctrl,
					history : r,
					size : 'lg',
					mapping : mapping
				})
			})

		}
	}
	sc.controller('StudentCtrl', StudentCtrl)
	function StudentCtrl($http) {
		var ctrl = this
		this.getInfo = function(id) {
			return $http.post(':sc:student/getInfo', {
				id : id
			}).success(function(r) {
				ctrl.info = r
				return r;
			})
		}
	}
	sc.controller('ValidatorCtrl', ValidatorCtrl)
	function ValidatorCtrl() {
		this.$dates = [];
		var ctrl = this
		this.validateDate = function(d, i) {
			ctrl.$dates[i] = d
			if (ctrl.$dates[i - 1] && (ctrl.$dates[i - 1] > d)) {
				return false;
			}
			if (ctrl.$dates[i + 1] && (ctrl.$dates[i + 1] < d)) {
				return false;
			}
			return true;
		}
	}
	function nyAchievementNav() {
		return {
			restrict : 'C',
			link : function($scope, $element, attrs) {
				var onAnimate = false;
				$element.on('mouseenter', function() {
					if (onAnimate) {
						return;
					}
					onAnimate = true;
					$element.animate({
						width : '150px'
					}, 300, function() {
						onAnimate = false;
						$element.find('a').show()
						$element.find('span').remove()
					})
				})
				$element.on('mouseleave', function() {
					if (onAnimate) {
						return;
					}
					onAnimate = true;
					$element.animate({
						width : '30px'
					}, 300, function() {
						$element.find('a').hide()
						$element.find('a').each(function(index, element) {
							onAnimate = false;
							var ico = $(element).attr('s-icon')
							var sp = $('<span/>').addClass('ny-achievement-icon ' + ico)
							$(element).before(sp)
						})
					})
				})
				setTimeout(function() {
					$element.trigger('mouseleave')
				}, 1600)

			}
		}
	}
	sc.directive('nyAchievementNav', nyAchievementNav)
})()